﻿using System.Windows;
using System.Windows.Controls;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;
using System.Collections.ObjectModel;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Controls
{
    /// <summary>
    /// Interaction logic for ParvisUserListControl.xaml
    /// </summary>
    public partial class ParvisUserListControl
    {
        public event RoutedEventHandler SelectedUserChanged;

        public ParvisUserListControl()
        {
            InitializeComponent();
            Loaded += ParvisUserListControl_Loaded;
        }

        void ParvisUserListControl_Loaded(object sender, RoutedEventArgs e)
        {
            Model.CollectionChanged += Model_CollectionChanged;
        }

        void Model_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewParvisUsers.SelectedIndex = 0;
        }

        public ObservableCollection<UserPresentationModel> Model
        {
            get { return DataContext as ObservableCollection<UserPresentationModel>; }
            set { DataContext = value; }
        }

        private void listViewParvisUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedUserChanged != null)
                SelectedUserChanged(this, new RoutedEventArgs());
        }

        public UserPresentationModel SelectedItem
        {
            get
            {
                if (listViewParvisUsers.SelectedItem != null)
                    return listViewParvisUsers.SelectedItem as UserPresentationModel;

                return null;
            }
        }
    }
}